import hashlib
import io

from django.http import HttpResponse

from WebSite.views.captcha import Captcha
from predict.settings import SESSION_KEY_CAPTCHA

SALT = '6be88297-9111-4973-8b55-05b9c07862d1'


# 验证码模块
def captcha(request):
	code_img, captcha_code = Captcha.create_captcha()
	buf = io.BytesIO()
	code_img.save(buf, 'jpeg')
	request.session[SESSION_KEY_CAPTCHA] = captcha_code
	return HttpResponse(buf.getvalue(), 'image/jpeg')


# 字符串加盐转md5
def strtomd5(origin):
	hl = hashlib.md5()
	hl.update((origin + SALT).encode(encoding='utf-8'))
	return hl.hexdigest()
